package team.hyznrj.studentsys.service.impl;

import java.util.List;

import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import team.hyznrj.studentsys.dao.mapper.PEMapMapper;
import team.hyznrj.studentsys.entity.DataReport;
import team.hyznrj.studentsys.entity.PEMap;
import team.hyznrj.studentsys.entity.PEMapExample;
import team.hyznrj.studentsys.enums.DataReportEnum;
import team.hyznrj.studentsys.exception.DataBaseOperationError;
import team.hyznrj.studentsys.exception.DataNullException;
import team.hyznrj.studentsys.service.PEMapService;
import team.hyznrj.studentsys.utils.ValueUtil;
@Service
public class PEMapServiceImpl implements PEMapService {
	@Autowired
	PEMapMapper mapper;
		
	@Override
	public DataReport addPE(String value) {
		String str = ValueUtil.noneEmpty(value, null, true);
		PEMap entity=new PEMap();
		entity.setPeId(new ObjectId().toString());
		entity.setPeName(str);
		if(mapper.insert(entity)>0){
			return new DataReport(DataReportEnum.SUCCESS,"体育课字段添加成功",entity,true);
		}else{
			throw new DataBaseOperationError("体育馆字段添加失败", DataReportEnum.INSERT_ERROR);
		}
	}

	@Override
	public DataReport deletePE(String id) {
		id = ValueUtil.noneEmpty(id, null, true);
		PEMapExample example=new PEMapExample();
		example.createCriteria().andPeIdEqualTo(id);
		if(mapper.deleteByExample(example)>0){
			return new DataReport(DataReportEnum.SUCCESS,"体育课字段删除成功",null,true);
		}else{
			throw new DataBaseOperationError("体育馆字段删除失败", DataReportEnum.DELETE_ERROR);
		}
	}

	@Override
	public DataReport updatePE(PEMap entity) {
		if(entity==null||StringUtils.isEmpty(entity.getPeId())||StringUtils.isEmpty(entity.getPeName())){
			throw new DataNullException("参数不能为空");
		}
		PEMapExample example=new PEMapExample();
		example.createCriteria().andPeIdEqualTo(entity.getPeId());
		if(mapper.updateByExample(entity, example)>0){
			return new DataReport(DataReportEnum.SUCCESS,"体育课字段更新成功",entity,true);
		}else{
			throw new DataBaseOperationError("体育馆字段更新失败", DataReportEnum.UPDATE_ERROR);
		}
	}

	@Override
	public List<PEMap> getAllPE() {
		return  mapper.selectByExample(null);
	}
}
